/*
 * @Author: jiejie
 * @Github: https://github.com/jiejieTop
 * @LastEditTime: 2020-06-17 14:35:29
 * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
 */
#include <stdio.h>
#include <stdint.h>
#include "mqttclient.h"
#include "platform_timer.h"

mqtt_client_t *client = NULL;

/*special set to no_ca_crt, tls will not do verify crt.*/
/*this string fix to this. do not changed it.*/
static const char *no_ca_crt = {"no_ca_crt"};

static const char *ca_crt = {
"-----BEGIN CERTIFICATE-----\r\n"
"MIIEAzCCAuugAwIBAgIUBY1hlCGvdj4NhBXkZ/uLUZNILAwwDQYJKoZIhvcNAQEL\r\n"
"BQAwgZAxCzAJBgNVBAYTAkdCMRcwFQYDVQQIDA5Vbml0ZWQgS2luZ2RvbTEOMAwG\r\n"
"A1UEBwwFRGVyYnkxEjAQBgNVBAoMCU1vc3F1aXR0bzELMAkGA1UECwwCQ0ExFjAU\r\n"
"BgNVBAMMDW1vc3F1aXR0by5vcmcxHzAdBgkqhkiG9w0BCQEWEHJvZ2VyQGF0Y2hv\r\n"
"by5vcmcwHhcNMjAwNjA5MTEwNjM5WhcNMzAwNjA3MTEwNjM5WjCBkDELMAkGA1UE\r\n"
"BhMCR0IxFzAVBgNVBAgMDlVuaXRlZCBLaW5nZG9tMQ4wDAYDVQQHDAVEZXJieTES\r\n"
"MBAGA1UECgwJTW9zcXVpdHRvMQswCQYDVQQLDAJDQTEWMBQGA1UEAwwNbW9zcXVp\r\n"
"dHRvLm9yZzEfMB0GCSqGSIb3DQEJARYQcm9nZXJAYXRjaG9vLm9yZzCCASIwDQYJ\r\n"
"KoZIhvcNAQEBBQADggEPADCCAQoCggEBAME0HKmIzfTOwkKLT3THHe+ObdizamPg\r\n"
"UZmD64Tf3zJdNeYGYn4CEXbyP6fy3tWc8S2boW6dzrH8SdFf9uo320GJA9B7U1FW\r\n"
"Te3xda/Lm3JFfaHjkWw7jBwcauQZjpGINHapHRlpiCZsquAthOgxW9SgDgYlGzEA\r\n"
"s06pkEFiMw+qDfLo/sxFKB6vQlFekMeCymjLCbNwPJyqyhFmPWwio/PDMruBTzPH\r\n"
"3cioBnrJWKXc3OjXdLGFJOfj7pP0j/dr2LH72eSvv3PQQFl90CZPFhrCUcRHSSxo\r\n"
"E6yjGOdnz7f6PveLIB574kQORwt8ePn0yidrTC1ictikED3nHYhMUOUCAwEAAaNT\r\n"
"MFEwHQYDVR0OBBYEFPVV6xBUFPiGKDyo5V3+Hbh4N9YSMB8GA1UdIwQYMBaAFPVV\r\n"
"6xBUFPiGKDyo5V3+Hbh4N9YSMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQEL\r\n"
"BQADggEBAGa9kS21N70ThM6/Hj9D7mbVxKLBjVWe2TPsGfbl3rEDfZ+OKRZ2j6AC\r\n"
"6r7jb4TZO3dzF2p6dgbrlU71Y/4K0TdzIjRj3cQ3KSm41JvUQ0hZ/c04iGDg/xWf\r\n"
"+pp58nfPAYwuerruPNWmlStWAXf0UTqRtg4hQDWBuUFDJTuWuuBvEXudz74eh/wK\r\n"
"sMwfu1HFvjy5Z0iMDU8PUDepjVolOCue9ashlS4EB5IECdSR2TItnAIiIwimx839\r\n"
"LdUdRudafMu5T5Xma182OC0/u/xRlEm+tvKGGmfFcN0piqVl8OrSPBgIlb+1IKJE\r\n"
"m/XriWr/Cq4h/JfB7NTsezVslgkBaoU=\r\n"
"-----END CERTIFICATE-----\r\n"
};

static void sub_topic_handle1(void* client, message_data_t* msg)
{
    (void) client;
    KAWAII_MQTT_LOG_I("-----------------------------------------------------------------------------------");
    KAWAII_MQTT_LOG_I("%s:%d %s()...\ntopic: %s\nmessage:%s", __FILE__, __LINE__, __FUNCTION__, msg->topic_name, (char*)msg->message->payload);
    KAWAII_MQTT_LOG_I("-----------------------------------------------------------------------------------");
}

static int mqtt_publish_handle1(mqtt_client_t *client)
{
    mqtt_message_t msg;
    memset(&msg, 0, sizeof(msg));

    msg.qos = QOS0;
    msg.payload = (void *)"this is a kawaii mqtt test ...";

    return mqtt_publish(client, "rtt-topic", &msg);
}

void mqtt_sample1(void)
{
    //mqtt_client_t *client = NULL;
	int client_status = 0;

    mqtt_sleep_ms(6000);

    mqtt_log_init();

    KAWAII_MQTT_LOG_I("%s()... mqtt test begin...", __FUNCTION__);

    client = mqtt_lease();

    mqtt_set_host(client, "test.mosquitto.org");
    mqtt_set_port(client, "1883");
    //mqtt_set_user_name(client, "asr-mqtt");
    //mqtt_set_password(client, "asr-mqtt");
    mqtt_set_client_id(client, "asr-mqtt-a5a5");
    mqtt_set_clean_session(client, 1);

    mqtt_connect(client);
    mqtt_subscribe(client, "test", QOS0, sub_topic_handle1);

    while (1) {
        mqtt_publish_handle1(client);
        mqtt_sleep_ms(4 * 1000);

		mqtt_query(client, MQTT_CLIENT_STATE, &client_status);
		if (CLIENT_STATE_INVALID == client_status) {
			mqtt_release(client);
			break;
		}
    }
}

static int mqtt_publish_handle2(mqtt_client_t *client)
{
    mqtt_message_t msg;
    memset(&msg, 0, sizeof(msg));

    msg.qos = QOS0;
    msg.payload = (void *)"open together!!";

    return mqtt_publish(client, "test", &msg);
}

void mqtt_sample2(void)
{
    //mqtt_client_t *client = NULL;
	int client_status = 0;

    mqtt_log_init();
    KAWAII_MQTT_LOG_I("%s()... mqtt test begin...", __FUNCTION__);

    client = mqtt_lease();

    mqtt_set_host(client, "test.mosquitto.org");
    mqtt_set_port(client, "8883");
    mqtt_set_ca(client, (char*) ca_crt);
	//mqtt_set_ca(client, (char*) no_ca_crt);
    mqtt_set_client_id(client, "asr-mqtt-a5a5");
    mqtt_set_clean_session(client, 1);

    mqtt_connect(client);
    mqtt_subscribe(client, "test", QOS0, sub_topic_handle1);

    while (1) {
        mqtt_sleep_ms(4 * 1000);
		mqtt_publish_handle2(client);

		mqtt_query(client, MQTT_CLIENT_STATE, &client_status);
		if (CLIENT_STATE_INVALID == client_status) {
			mqtt_release(client);
			break;
		}
    }
}

void mqtt_sample(int test_num)
{

    if (0 == test_num) {
        /*do mqtt_sample 1*/
        mqtt_sample1();
    } else if (1 == test_num) {
        /*do mqtt_sample 2*/
        mqtt_sample2();
    } else {
        /* @TBD */
    }
}
